
exports.add=[{
	 _type:"model_methods",
	_config: {name:"MarketField"},

	
	CalcCustom: function() {
		var rr=this.GetRR();
		if (this.id) {
			var old=rr.site.models.MarketField.Get(this.id);
			if (old.datatype_id!=this.datatype_id) throw new Error("datatype change is forbidden!");
			if (old.group_id!=this.group_id) throw new Error("group change is forbidden!");
			if (old.cid_id && old.cid_id!=this.cid_id) throw new Error("cid change is forbidden!");
		}
		var dt=rr.site.models.MarketFieldDatatype.Get(this.datatype_id);
		if (!this.cid) {
			if (!this.group_id) throw new Error("group_id is empty");
			if (!this.datatype_id) throw new Error("datatype_id is empty");
			if (dt.prefix) {
				var data=rr.site.sql.execute_and_fetch_one("market_fields/calc_next_cid",{datatype_id:this.datatype_id,group_id:this.group_id});
				this.cid=data.next_cid;
				var grp=rr.site.models.MarketFieldGroup.Get(this.group_id);
				if (!this.cid) throw new Error("cid is empty");
				if (!this.cid.toString().match(/^\d+$/)) throw new Error("cid not a number - "+this.cid);
				rr.site.sql.execute_single("alter table Tmarket_item_fields_"+dt.prefix+" add "+(grp.category_id?"fldcat":"fldany")+this.cid+" "+dt.crsql);
			} else {
				this.cid=-1;
			}
		}
	}
}];


